<template>
  <div class="app-container">
    <wise-crud ref="wise-crud" :urls="urls" :fields="fields"></wise-crud>
  </div>
</template>

<script>
import WiseCrud from '@/components/Crud/wiseCrud'
import { postHandle } from '@/api'
export default {
  name: 'DepartmentManage',
  components: { WiseCrud },
  data() {
    return {
      urls: {
        list: '/api/v1/welder-uac-second/user-information/',
        add: '/api/v1/welder-uac-second/user-information-comb/create-user-info/',
        show: '/api/v1/welder-uac-second/user-information/show/',
        update: '/api/v1/welder-uac-second/user-information-comb/update-user-info/',
        delete: '/api/v1/welder-uac-second/user-information-comb/remove-user-info/'
      },
      fields: [
        { label: '序号', type: 'index', use: ['table'] },
        {
          label: '个人头像',
          prop: 'picture',
          type: 'file',
          use: ['table', 'add', 'view', 'edit', 'search']
        },
        {
          label: '真实姓名',
          prop: 'username',
          type: 'text',
          maxlength: 20,
          rules: [
            { required: true, message: '请输入真实姓名', trigger: 'blur' }
          ],
          placeholder: '请输入真实姓名',
          use: ['table', 'add', 'view', 'edit', 'search']
        },
        {
          label: '联系方式',
          prop: 'phone',
          type: 'text',
          maxlength: 11,
          rules: [
            { required: true, message: '请输入登录手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '请输入11位手机号码', trigger: 'blur' },
            {
              pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
              message: '请输入正确的手机号码'
            }
          ],
          placeholder: '请输入登录手机号',
          use: ['table', 'add', 'view', 'edit', 'search']
        },
        {
          label: '备注',
          prop: 'remark',
          type: 'textarea',
          placeholder: '请输入简介',
          use: ['add', 'view', 'edit']
        },
        { label: '操作', type: 'handle', use: ['table'], handle: ['edit', 'view', 'delete'], tableWidth: 320 }
      ],
      drawer: false,
      filterMethod(query, item) {
        return item.name.indexOf(query) > -1
      },
      userInfo: {},
      obj: {
        roleCodes: []
      },
      roleList: [],
      rules: {
        roleCodes: [
          { type: 'array', required: true, message: '请选择角色', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    settingHandle(row) {
      this.obj.roleCodes = []
      this.userInfo = row
      this.getUserRoleList(row.userId)
      this.getRoleList()
      this.drawer = true
    },
    getRoleList() {
      const req = {
        d: {
          filters: [],
          fields: [],
          page: {
            p: 1,
            ps: 9999
          }
        }
      }
      postHandle('/api/v1/welder-uac-second/role', req).then(response => {
        this.roleList = response.result
      })
    },
    getUserRoleList(userId) {
      const req = {
        d: {
          filters: [],
          fields: [],
          page: {
            p: 1,
            ps: 9999
          }
        }
      }
      postHandle(`/api/v1/welder-uac-second/user-role-comb/get-user-role-by-userid/${userId}`, req).then(response => {
        let data = response.result
        if (data && data.length > 0) {
          this.obj.roleCodes = data.map(item => item.roleCode)
        }
      }).catch(error => {
        this.loading = false
        const data = error.response.data
        for (const key in data) {
          this.$message.error(this.$t(key + '.' + data[key]))
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postHandle(`/api/v1/welder-uac-second/user-role-comb/configure-user-role/${this.userInfo.userId}`, { obj: this.obj }).then(response => {
            this.drawer = false
          }).catch(error => {
            this.loading = false
            const data = error.response.data
            for (const key in data) {
              this.$message.error(this.$t(key + '.' + data[key]))
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style>

</style>
